package com.songlin.testv2ex.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.songlin.testv2ex.R;
import com.songlin.testv2ex.data.GlobalData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 松林 on 2015/10/2.
 */
public class FirstFragment extends Fragment {

    int[] replies;
    long[] created;
    String[] title, node_title,member_username,timeToShow;

    ListView listView;

    /*private Button button;
    private TextView textView;*/

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment1,container,false);
        listView = (ListView) rootView.findViewById(R.id.list_view);
      /*button = (Button) rootView.findViewById(R.id.button);
        textView = (TextView) rootView.findViewById(R.id.textview);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {*//*
                for (int i = 0; i < GlobalData.Lastest_Topics.size(); i++) {
                    textView.setText(textView.getText().toString()+"+"+GlobalData.Lastest_Topics.get(i).replies);
                    textView.setText(textView.getText().toString()+"++"+GlobalData.Lastest_Topics.get(i).created);
                    textView.setText(textView.getText().toString()+"++"+GlobalData.Lastest_Topics.get(i).title);
                    textView.setText(textView.getText().toString()+"++"+GlobalData.Lastest_Topics.get(i).node.title);
                    textView.setText(textView.getText().toString()+"++"+GlobalData.Lastest_Topics.get(i).member.username);
                }*//*
            }
        });*/

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d(" - - - - - - - - - - "," - - - - - - " + position);

                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(GlobalData.Lastest_Topics.get(position).url);
                intent.setData(content_url);
                startActivity(intent);
            }
        });

        replies = new int[17];
        created = new long[17];
        title = new String[17];
        node_title = new String[17];
        member_username = new String[17];
        timeToShow = new String[17];

        for (int i = 0; i < GlobalData.Lastest_Topics.size(); i++) {
            replies[i] = GlobalData.Lastest_Topics.get(i).replies;
            created[i] =  GlobalData.Lastest_Topics.get(i).created;
            title[i] = GlobalData.Lastest_Topics.get(i).title;
            node_title[i] = GlobalData.Lastest_Topics.get(i).node.title;
            member_username[i] = GlobalData.Lastest_Topics.get(i).member.username;
        }

        long currentTime = System.currentTimeMillis();
        for (int i = 0; i < created.length; i++) {
            created[i] = (currentTime / 1000) - created[i];
            String tempTime = "未知时间";
            int day = (int) (created[i] / (24 * 3600));
            int hour = (int) ((created[i] / 3600) % 24);
            int min = (int) ((created[i] / 60) % 60);
            int sec = (int) (created[i] % 60);
            if ( day > 1) {
                tempTime = day + "天前";
            } else if ( day > 0) {
                tempTime = day + "天前" + hour + "小时前";
            } else if ( hour > 0) {
                tempTime = hour + "小时前";
            } else if ( min > 0) {
                tempTime = min + "分钟前";
            } else {
                tempTime = sec + "秒前";
            }
            timeToShow[i] = "发布于" + tempTime;
        }
        List<Map<String,Object>> listItems = new ArrayList<Map<String, Object>>();
        for ( int i = 0; i < title.length; i++) {
            Map<String,Object> listItem = new HashMap<String, Object>();
            listItem.put("title",title[i]);
            listItem.put("nodeTitle", node_title[i]);
            listItem.put("memberUsername",member_username[i]);
            listItem.put("replies",replies[i]);
            listItem.put("time",timeToShow[i]);
            listItems.add(listItem);
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(
                getActivity().getApplicationContext(),
                listItems,
                R.layout.item_topics_list,
                new String[] { "title", "nodeTitle", "memberUsername", "replies", "time" },
                new int[] { R.id.title,R.id.node_title, R.id.name, R.id.replies_tv, R.id.time,  });
        listView.setAdapter(simpleAdapter);

        return rootView;
    }
}